//
//  DXEmotionViewModel.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/16.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

let emotionLatestArchiverPath = keyedArchiverPath("emotionLatest.data")
class DXEmotionViewModel: NSObject {
    
    static let sharedEmoton:DXEmotionViewModel = DXEmotionViewModel()
    
//    对外界提供的数据接口
    var emotions:[[[DXEmotonModel]]]?{
    
    return [emotonDeLatests,emotonDefaults,emotonEmojis,emotonLxhs]
    }
    
    
    var lastestEmotion:[DXEmotonModel]!
    
    var emotonDeLatests:[[DXEmotonModel]]{
        return getSectionAarry(lastestEmotion)
    }
    
    var emotonDefaults:[[DXEmotonModel]]!
    
    var emotionDefault:[DXEmotonModel]!

    var emotonEmojis:[[DXEmotonModel]]!
    var emotonEmoji:[DXEmotonModel]!

    var emotonLxhs:[[DXEmotonModel]]!
    var emotonLxh:[DXEmotonModel]!

    
    override init() {
        super.init()
   let unarchiver = NSKeyedUnarchiver.unarchiveObjectWithFile(emotionLatestArchiverPath)
       if  unarchiver == nil {
        lastestEmotion = [DXEmotonModel]()
       }else{
        
        lastestEmotion = unarchiver as! [DXEmotonModel]
        }
        emotionDefault = getEmotonInfo("default/info.plist")
        emotonDefaults = getSectionAarry(emotionDefault)
        emotonEmoji = getEmotonInfo("emoji/info.plist")
        emotonEmojis = getSectionAarry(emotonEmoji)
        emotonLxh = getEmotonInfo("lxh/info.plist")
        emotonLxhs = getSectionAarry(emotonLxh)
    }
    
    // MARK: - 查找是否有与特殊的字符想匹配的表情模型数据
    
    func matchEmotionWithString(matchStr:String)->DXEmotonModel?{
       let defaultResult = enumEmotions(emotionDefault,matchStr:matchStr)
        if defaultResult == nil{
        let lxhResult = enumEmotions(emotonLxh,matchStr:matchStr)
            if lxhResult != nil {
            return lxhResult
            }else{
            return nil
            }
        }else{
        return defaultResult
        }
        
    }
    private func enumEmotions(emotionArray:[DXEmotonModel],matchStr:String)->DXEmotonModel?{
        
        for emotionModel in emotionArray {
            if (matchStr as NSString).isEqualToString(emotionModel.chs!) || (matchStr as NSString).isEqualToString(emotionModel.cht!){
                return emotionModel
            }
        }
      return nil
    }
    
    // MARK: - 保存最近表情
    func saveLatestEmotions(emotionModel:DXEmotonModel){
        if lastestEmotion.count > 19 {
        lastestEmotion.removeLast()
        }
       let array = lastestEmotion as Array
//        遍历数组查找在数组中是否存在当前模型一样的表情,重写isEqualTo方法
       let index = array.indexOf(emotionModel)
        if index != NSNotFound && index != nil{
        lastestEmotion.removeAtIndex(index!)
        }
        lastestEmotion.insert(emotionModel, atIndex: 0)
       
        
        NSKeyedArchiver.archiveRootObject(lastestEmotion, toFile: emotionLatestArchiverPath)
    }

    
    // MARK: - 把一个大数组拆分拆分为多个20个元素的数组
    
    private func getSectionAarry(largeArray:[DXEmotonModel])->[[DXEmotonModel]]{
    
        var tempArray:[[DXEmotonModel]] = [[DXEmotonModel]]()
    let sectionNum = (largeArray.count - 1 ) / emotonsPageOfNum + 1
        
        for i in 0..<(sectionNum - 1){
        
            let range = NSMakeRange(i*emotonsPageOfNum, emotonsPageOfNum)
            let subArray = (largeArray as NSArray).subarrayWithRange(range) as! [DXEmotonModel]
            tempArray.append(subArray)
        }
        let local = (sectionNum - 1)*emotonsPageOfNum
        let lenth = largeArray.count - local
        
        let range = NSMakeRange(local, lenth)
        let subArray = (largeArray as NSArray).subarrayWithRange(range) as! [DXEmotonModel]
        tempArray.append(subArray)

    return tempArray
    }
    
    
//    MARK:从本地读取表情
    func getEmotonInfo(filePath:String)->[DXEmotonModel]{
//        let path = bundelPath("default/info.plist")
    let path = EmotionBundel.pathForResource(filePath, ofType: nil)
        

       
        let imgPath = ( filePath  as NSString ).stringByDeletingLastPathComponent
//    logPrint(demo)
    let dictArray = NSArray(contentsOfFile: path!)
        
        var tempArray = [DXEmotonModel]()
        for dict in dictArray!{
       let model = DXEmotonModel(dict: dict as! [String : AnyObject])
        tempArray.append(model)
        
            if model.png != nil {
           model.img = "\(imgPath)/\(model.png!)"
            }
        }
       return tempArray
    }
//    懒加载表情Bundel
    lazy var EmotionBundel:NSBundle = {
        let tempPath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        let bundel = NSBundle(path: tempPath)
        
        return bundel!
    }()

}
